package cn.skyclass.controls
{
    import flash.events.Event;
    import flash.external.ExternalInterface;
    import flash.geom.Point;
	
	/**
	 * 多媒体编辑器
	 * 功能：在 flash 内部完成和 jsp 多媒体编辑器的交互
	 * 
	 * 使用方法：
	 * 1、在需要使用此控件的目标网页中引用 htmleditor.js 文件。
	 * 2、在 flash 标签部分添加 wmode="opaque" 属性。	
	 * 3、拷贝 htmleditor.jsp 到某个位置，将此控件的 source 属性指向它
	 */
	[Bindable]
	public class HtmlEditor extends OuterWindow
	{
		/**
		 * 标识 html 页面中对应的 div 的 id ，
		 * HtmlEditor 将使用此 div 来加载多媒体编辑器的网页
		 */
		public var iframeId:String = "iframeEditor";
		
		
		public function get htmlContent():String{
			return ExternalInterface.call("getContent", this.iframeId);
		}		
		public function set htmlContent(content:String):void{
			if(content == null){
				content = "";
			}
			ExternalInterface.call("setContent", this.iframeId, content);
		}			
				
		public function get htmlEditorUploadUrl():String {
			return ExternalInterface.call("getHtmlEditorUploadUrl", this.iframeId);
		}
		
		public function get htmlEditorUploadPhysicalPath():String {
			return ExternalInterface.call("getHtmlEditorUploadPhysicalPath", this.iframeId);
		}
        /**
         * Move iframe through ExternalInterface.  The location is determined using localToGlobal()
         * on a Point in the Canvas.
         **/
        protected function moveEditor():void{
            var localPt:Point = new Point(0, 0);
            var globalPt:Point = this.localToGlobal(localPt);
            ExternalInterface.call("moveEditor", this.iframeId, globalPt.x, globalPt.y, this.width, this.height);
        }

 		private var _source:String;
        public function get source():String{
            return _source;
        }
        public function set source(source:String):void{
            if (source){
                if (! ExternalInterface.available){
                	var msg:String = "ExternalInterface is not available in this container.";
                	msg += " Internet Explorer ActiveX, Firefox, Mozilla 1.7.5 and greater,";
                	msg += " or other browsers that support NPRuntime are required.";
                    throw new Error(msg);
                }
                _source = source;
                ExternalInterface.call("loadEditor", this.iframeId, source);                
                this.visible = true;
                this.setVisibility(true);
                this.moveEditor();
            }
        }

		override public function get visible():Boolean{
			return super.visible;
		}
        override public function set visible(visible:Boolean):void{
            super.visible = visible;
            ExternalInterface.call("setEditorVisibility", this.iframeId, visible);            
        }
		
		public function setVisibility(visible:Boolean):void{
			super.visible = visible;
            ExternalInterface.call("setEditorVisibility", this.iframeId, visible);			
		}
        
        override protected function moveOuterWindow(e:Event=null):void{
			this.moveEditor();
		}
		
	}
}